import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/auth/login_page.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/store_utils.dart';
import './pages/index_page.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluwx/fluwx.dart' as fluwx;

//void main() => runApp(MyApp());

SharedPreferences sp;

Future main() async {

  try {
    _initFluwx();

    //用户
    sp = await SharedPreferences.getInstance();
    var val = sp.get(SharedPreferenceUtil.USER_ID);
    runApp(MyApp(val));
  } on IntegerDivisionByZeroException {
    print('Division by zero.');
  }


}
_initFluwx() async {
  await fluwx.registerWxApi(
      appId: "wx5ed2164b0506ce1d",//wx06d65621385fbbc8 //wx5ed2164b0506ce1d
      doOnAndroid: true,
      doOnIOS: false,
      universalLink: "https://your.univerallink.com/link/");
  var result = await fluwx.isWeChatInstalled();
  print("is installed $result");
}

class MyApp extends StatelessWidget  {

  var userId;
  MyApp(this.userId);

  @override
  Widget build(BuildContext context) {

    Map<String, WidgetBuilder> routes = Map();
    Map<String, WidgetBuilder> staticRoutes = {
      '/': (context) => IndexPage(userId),
    };
    routes.addAll(staticRoutes);

    return Container(
      child: MaterialApp(
        title: "阳光北亭管理",
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.red, //Color.fromRGBO(255, 0, 0, 1)
        ),
        home:IndexPage(userId),
//        home: userId == null
//            ? LoginPage()
//            : IndexPage(),
        routes: <String, WidgetBuilder>{
          '/login': (BuildContext context) => LoginPage(),
          '/home': (BuildContext context) => IndexPage(userId)
        },
        localizationsDelegates: [
          //此处
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          //此处
          const Locale('zh', 'CH'),
          const Locale('en', 'US'),
        ],

      ),
    );
  }


//  void _initJPush() async {
//    await FlutterJPush.startup();
//    print("初始化jpush成功");
//
//    // 获取 registrationID
//    var registrationID =await FlutterJPush.getRegistrationID();
//    print(registrationID);
//
//    // 注册接收和打开 Notification()
//    _initNotification();
//  }
//
//  void _initNotification() async {
//    FlutterJPush.addReceiveNotificationListener(
//            (JPushNotification notification) {
//          print("收到推送提醒: $notification");
//        }
//    );
//
//    FlutterJPush.addReceiveOpenNotificationListener(
//            (JPushNotification notification) {
//          print("打开了推送提醒: $notification");
//        }
//    );
//  }


}



